
/****
 * Copyright (C) 2006 dNux Team
 *                                                            ****
 * This program is free software; you can redistribute it and/or *
 * modify under the terms of the GNU General Public License as   *
 * published by the Free Software Foundation;                    *
 * either version 2 of the License.                              *
 *                                                               *
 * This program is distributed in the hope that it will be       *
 * useful, but WITHOUT ANY WARRANTY; without even the implied    *
 * warranty of without even the implied waranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.          *
 * See the GNU General Public License for more details.          *
 *                                                               *
 * You should have received a copy of the                        *
 * GNU General Public License                                    *
 * along with this program; if not, write to the                 *
 * Free Software Foundation, Inc.,                               *
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. *
 *                                                               *
 ****                                                         ****/

#include <dpkg.h>

void
d_pkg_install_data(d_pkg_t *pkg, d_error_t **error)
{
  char *prefix;
  pid_t pid;

  int pipe_bzip2[2];
  int len, err;
  
  char buff[2048];

  FILE *file;
  BZFILE *bzip2;
  TAR *tar;
  
  if (pipe(pipe_bzip2) == -1) {
    d_error(LIBDPKG, "pipe error");
    goto error;
  }
  
  pid = fork();
  //error
  if (pid == -1) {

  }
  //son
  else if(pid == 0) {
    close(pipe_bzip2[0]); //read
    
    file = fopen("data.tar.bz2", "r");
    if (file == NULL) goto bzip2_error;
    bzip2 = BZ2_bzReadOpen(&err, file, 0, 0, NULL, 0);
    if (err != BZ_OK) goto bzip2_error;
    do {
      len = BZ2_bzRead(&err, bzip2, buff, sizeof(buff));
      write(pipe_bzip2[1], buff, len);
    } while(len > 0);
    
    BZ2_bzReadClose(&err, bzip2);
    if (err != BZ_OK) goto bzip2_error;

    goto ok;
  bzip2_error:
    printf("bzip2 error\n");
  ok:
    close(pipe_bzip2[1]); //write
    exit(EXIT_SUCCESS);
  }
  //father
  else if (pid > 0) {
    close(pipe_bzip2[1]); //write
    if (tar_fdopen(&tar, pipe_bzip2[0], "data.tar", NULL, O_RDONLY, 0644, 0) == -1) {
      d_error(LIBDPKG, "tar_fdopen error.");
      goto tar_error;
    }

    prefix = "/";
    if (pkg->install_prefix) prefix = pkg->install_prefix;
    if (tar_extract_all(tar, prefix) == -1) {
      d_error(LIBDPKG, "tar_extract_all error.");
      goto tar_error;
    }
    if (tar_close(tar) == -1) {
      d_error(LIBDPKG, "tar_close error.");
      goto tar_error;
    }
    
  tar_error:
    close(pipe_bzip2[0]); //read
  }

 error:
  return;
}

void
d_pkg_install_unpack(d_pkg_t *pkg, d_error_t **error)
{
  TAR *tar;
  
  if (tar_open(&tar, pkg->file, NULL, O_RDONLY, 0640, 0) == -1) {
    d_error(LIBDPKG, "tar_open error.");
    goto error;
  }
  if (tar_extract_all(tar, "./") == -1) {
    d_error(LIBDPKG, "tar_extract_all error.");
    goto error;
  }
  if (tar_close(tar) == -1) {
    d_error(LIBDPKG, "tar_close error");
    goto error;
  }

 error:
  return;
}

void
d_pkg_install(d_pkg_t *pkg, d_error_t **error)
{
  d_pkg_install_unpack(pkg, error);
  if (*error) goto error;

  d_pkg_install_data(pkg, error);
  if (*error) goto error;

  unlink("data.tar.bz2");

 error:
  return;
}
